var AudioManager = {};

AudioManager.eventHandlers = {};

AudioManager.marks = {};

AudioManager.baseInitWavesurfer = function (prefix, is_background, enable_marker_sliders) {
    filename = AudioManager.currentFilename;
    if (is_background) {
        filename = AudioManager.currentBackgroundFilename;
    }
    
    var wavesurfer = Object.create(WaveSurfer);
    
    var options = {
        container     : document.querySelector('#'+prefix+'-waveform'),
        waveColor     : 'violet',
        progressColor : 'purple',
        loaderColor   : 'purple',
        cursorColor   : 'navy',
        markerWidth   : 2
    };

    if (location.search.match('scroll')) {
        options.minPxPerSec = 100;
        options.scrollParent = true;
    }

    if (location.search.match('normalize')) {
        options.normalize = true;
    }

    /* Progress bar */
    var progressDiv = $('#'+prefix+'-progress-bar');
    var progressBar = progressDiv.find('.progress-bar');
    wavesurfer.on('loading', function (percent, xhr) {
        progressDiv.show();
        progressBar.css('width', percent + '%');
    });
    wavesurfer.on('ready', function () {
        progressDiv.hide();
        
        $('#'+prefix+'-duration').html(Number(wavesurfer.backend.getDuration()).toFixed(2));
        
        if (enable_marker_sliders) {
            // Делаем слайдеры
            wavesurfer.mark({
                id: 'begin',
                color: 'rgba(255, 0, 0, 0.5)',
                position: null,
                percentage: 0
            });  
            wavesurfer.mark({
                id: 'end',
                color: 'rgba(255, 0, 0, 0.5)',
                position: null,
                percentage: 100
            }); 
            
            var updateSliderDuration = function () {
                var begin = Number(wavesurfer.markers.begin.position).toFixed(2),
                    end   = Number(wavesurfer.markers.end.position).toFixed(2),
                    duration = Number(end - begin).toFixed(2);
                $('#'+prefix+'-slider-duration').find('.begin').html(begin);
                $('#'+prefix+'-slider-duration').find('.end').html(end);
                $('#'+prefix+'-slider-duration').find('.duration').html(duration);    
            }; 
            
            if (!$('#'+prefix+'-slider').length) {
                $('#'+prefix+'-waveform').append('<div id="'+prefix+'-slider" class="marker-slider"></div>');            
            }
            if (!$('#'+prefix+'-slider-duration').length) { 
                $('#'+prefix+'-waveform').append('<div id="'+prefix+'-slider-duration" class="slider-duration">Выделен фрагмент [<span class="begin"></span>, <span class="end"></span>] продолжительностью <span class="duration"></span> с.</div>');
            }
            updateSliderDuration(); 
            
            $('#'+prefix+'-slider').slider({
                range: true,
                min: 0,
                max: 1000,
                values: [0, 1000],
                slide: function(event, ui) {
                    wavesurfer.markers.begin.update({percentage: (ui.values[0] / 10)});
                    wavesurfer.markers.end.update({percentage: (ui.values[1] / 10)});
                    
                    updateSliderDuration();
                }
            });
                
        } 
    });

    // Init
    wavesurfer.init(options);
    
    $('#'+prefix+'-waveform').append('<div id="'+prefix+'-duration" class="audio-duration"></div>');
    
    // Load audio from URL
    wavesurfer.load(filename);
    
    AudioManager.eventHandlers[prefix+'-play'] = function () {
        wavesurfer.playPause();
    };
    
    /*    
        'back': function () {
            wavesurfer.skipBackward();
        },

        'forth': function () {
            wavesurfer.skipForward();
        },
    };  */

    AudioManager.initEventListeners(prefix);

    /*document.addEventListener('click', function (e) {
        var action = e.target.dataset && e.target.dataset.action;
        if (action && action in AudioManager.eventHandlers) {
            AudioManager.eventHandlers[action](e);
            
        }
    });  */
    
    // Flash mark when it's played over
    wavesurfer.on('mark', function (marker) {
        if (marker.timer) { return; }

        marker.timer = setTimeout(function () {
            var origColor = marker.color;
            marker.update({ color: 'yellow' });

            setTimeout(function () {
                marker.update({ color: origColor });
                delete marker.timer;
            }, 100);
        }, 100);
    });

    wavesurfer.on('error', function (err) {
        console.error(err);
    });
    
    return wavesurfer;     
};

AudioManager.initEventListeners = function (prefix) {
    $('#'+prefix+'-step *[data-action]').click(function (e) {
        var action = $(this).attr('data-action');
        if (action && action in AudioManager.eventHandlers) {
            AudioManager.eventHandlers[action]();
            //e.stopPropagation();           
        }    
    });      
};

AudioManager.steps = null;
AudioManager.setSteps = function (steps) {
    AudioManager.steps = steps; 
};

AudioManager.hash = null;
AudioManager.setAudioHash = function (hash) {
    AudioManager.hash = hash; 
};

AudioManager.currentFilename = null;
AudioManager.setCurrentFilename = function (filename) {
    AudioManager.currentFilename = filename; 
};

AudioManager.currentBackgroundFilename = null;
AudioManager.setCurrentBackgroundFilename = function (filename) {
    AudioManager.currentBackgroundFilename = filename; 
};

AudioManager.previousFilename = null;
AudioManager.setPreviousFilename = function (filename) {
    AudioManager.previousFilename = filename; 
};

AudioManager.currentStep = null;
AudioManager.setCurrentStep = function (step) {
    AudioManager.currentStep = step; 
};

AudioManager.initPlayer = function () {   
    // TODO сделать покрасивше

    /*if (AudioManager.currentStep == 'cut-fragment') {
        AudioManager.CutFragmentPlayer  = null;
        AudioManager.CutFragmentEdition = 1;
        AudioManager.initCutFragmentStep();
    } else */
    if (AudioManager.currentStep == 'remove-noise') {
        AudioManager.RemoveNoisePlayer  = null;
        AudioManager.RemoveNoiseEdition = 1;
        AudioManager.initRemoveNoiseStep();
    } else if (AudioManager.currentStep == 'first-compression') {
        AudioManager.BeforeFirstCompressionPlayer = null;
        AudioManager.AfterFirstCompressionPlayer  = null;
        AudioManager.FirstCompressionEdition = 1;
        AudioManager.initFirstCompressionStep();
    } else if (AudioManager.currentStep == 'choose-music') {
        AudioManager.ChooseMusicPlayer  = null;
        AudioManager.ChooseMusicEdition = 1;
        AudioManager.initChooseMusicStep();
    } else if (AudioManager.currentStep == 'merge-tracks') {
        AudioManager.BeforeMergeTracksTrackPlayer  = null;
        AudioManager.BeforeMergeTracksBackgroundPlayer  = null;
        AudioManager.AfterMergeTracksPlayer  = null;
        AudioManager.MergeTracksEdition = 1;
        AudioManager.initMergeTracksStep();
    } else if (AudioManager.currentStep == 'second-compression') {
        AudioManager.BeforeSecondCompressionPlayer = null;
        AudioManager.AfterSecondCompressionPlayer  = null;
        AudioManager.SecondCompressionEdition = 1;
        AudioManager.initSecondCompressionStep();
    } else if (AudioManager.currentStep == 'download') {
        AudioManager.initDownloadStep();
    }
       
};

/*AudioManager.initCutFragmentStep = function () {
    var step = AudioManager.currentStep;
    
    document.querySelector('#'+step+'-step').style.display = 'block';  
    
    AudioManager.CutFragmentPlayer = AudioManager.baseInitWavesurfer(step);
    
    AudioManager.CutFragmentBegin = null;
    AudioManager.CutFragmentEnd = null;
    
    AudioManager.eventHandlers[step+'-begin-mark'] = function () {
        AudioManager.CutFragmentBegin = AudioManager.CutFragmentPlayer.mark({
            id: 'up',
            color: 'rgba(255, 0, 0, 0.5)'
        });
    };
    AudioManager.eventHandlers[step+'-end-mark'] = function () {
        AudioManager.CutFragmentEnd = AudioManager.CutFragmentPlayer.mark({
            id: 'down',
            color: 'rgba(255, 0, 0, 0.5)'
        });
    };
    AudioManager.eventHandlers[step+'-trim-ends'] = function () {
        if (AudioManager.isBeginAndEndCorrect(AudioManager.CutFragmentBegin, AudioManager.CutFragmentEnd)) {
            var begin = AudioManager.CutFragmentBegin.position;
            var end   = AudioManager.CutFragmentEnd.position;
            
            $.post('/audio/cut_fragment_trim_ends', {
                        hash: AudioManager.hash,
                        begin: begin,
                        end: end,
                        edition: AudioManager.CutFragmentEdition,
                        filename: AudioManager.currentFilename 
            }, function (data) {
                    if (data.success) {
                        AudioManager.setCurrentFilename(data.filename);
                        AudioManager.CutFragmentPlayer.load(data.filename);
                        AudioManager.CutFragmentEdition++;      
                    }        
            }, 'json');    
        }
    };
    AudioManager.eventHandlers[step+'-next-step'] = function () {
        AudioManager.nextStep();        
    };    
         
};   */

AudioManager.initRemoveNoiseStep = function () {
    var step = AudioManager.currentStep;
    
    if (prev_step = AudioManager.getPrevStep()) {
        $('#'+prev_step+'-step').hide();    
    }
    $('#'+step+'-step').show();
    
    AudioManager.hideUndoButton();
    
    AudioManager.RemoveNoisePlayer = AudioManager.baseInitWavesurfer(step, false, true);
    
    AudioManager.RemoveNoiseBegin = null;
    AudioManager.RemoveNoiseEnd = null;
    
    AudioManager.eventHandlers[step+'-reduce-sound'] = function () {
        if (AudioManager.isBeginAndEndCorrect(AudioManager.RemoveNoisePlayer.markers.begin, AudioManager.RemoveNoisePlayer.markers.end)) {
            var begin = AudioManager.RemoveNoisePlayer.markers.begin.position;
            var end   = AudioManager.RemoveNoisePlayer.markers.end.position;
            
            AudioManager.shadowBody();
            
            $.post('/audio/remove_noise_reduce_sound', {
                        hash: AudioManager.hash,
                        begin: begin,
                        end: end,
                        edition: AudioManager.RemoveNoiseEdition,
                        filename: AudioManager.currentFilename
            }, function (data) {
                    if (data.success) {
                        AudioManager.unshadowBody();
                        
                        AudioManager.setPreviousFilename(AudioManager.currentFilename);
                        AudioManager.setCurrentFilename(data.filename);
                        AudioManager.updateUndoButton();
                        
                        AudioManager.RemoveNoisePlayer.load(data.filename);
                        AudioManager.RemoveNoiseEdition++;      
                    }        
            }, 'json');    
        }
            
    }
    AudioManager.eventHandlers[step+'-trim-center'] = function () {
        if (AudioManager.isBeginAndEndCorrect(AudioManager.RemoveNoisePlayer.markers.begin, AudioManager.RemoveNoisePlayer.markers.end)) {
            var begin = AudioManager.RemoveNoisePlayer.markers.begin.position;
            var end   = AudioManager.RemoveNoisePlayer.markers.end.position;
            
            AudioManager.shadowBody();
            
            $.post('/audio/remove_noise_trim_center', {
                        hash: AudioManager.hash,
                        begin: begin,
                        end: end,
                        edition: AudioManager.RemoveNoiseEdition,
                        filename: AudioManager.currentFilename
            }, function (data) {
                    if (data.success) {
                        AudioManager.unshadowBody();
                        
                        AudioManager.setPreviousFilename(AudioManager.currentFilename);
                        AudioManager.setCurrentFilename(data.filename);
                        AudioManager.updateUndoButton();
                        
                        AudioManager.RemoveNoisePlayer.load(data.filename);
                        AudioManager.RemoveNoiseEdition++;      
                    }        
            }, 'json');    
        }
            
    }
    AudioManager.eventHandlers[step+'-trim-ends'] = function () {
        if (AudioManager.isBeginAndEndCorrect(AudioManager.RemoveNoisePlayer.markers.begin, AudioManager.RemoveNoisePlayer.markers.end)) {
            var begin = AudioManager.RemoveNoisePlayer.markers.begin.position;
            var end   = AudioManager.RemoveNoisePlayer.markers.end.position;
            
            AudioManager.shadowBody();
            
            $.post('/audio/remove_noise_trim_ends', {
                        hash: AudioManager.hash,
                        begin: begin,
                        end: end,
                        edition: AudioManager.RemoveNoiseEdition,
                        filename: AudioManager.currentFilename
            }, function (data) {
                    if (data.success) {
                        AudioManager.unshadowBody();
                        
                        AudioManager.setPreviousFilename(AudioManager.currentFilename);
                        AudioManager.setCurrentFilename(data.filename);
                        AudioManager.updateUndoButton();
                        
                        AudioManager.RemoveNoisePlayer.load(data.filename);
                        AudioManager.RemoveNoiseEdition++;      
                    }        
            }, 'json');    
        }    
    }
    AudioManager.eventHandlers[step+'-next-step'] = function () {
        AudioManager.RemoveNoisePlayer.pause();
        AudioManager.nextStep();        
    };   
}

AudioManager.initFirstCompressionStep = function () {
    var step = AudioManager.currentStep;
    var before_step = 'before-' + step;
    var after_step  = 'after-' + step;
    
    if (prev_step = AudioManager.getPrevStep()) {
        $('#'+prev_step+'-step').hide();    
    }
    $('#'+step+'-step').show();
    
    AudioManager.hideUndoButton();
    
    AudioManager.BeforeFirstCompressionPlayer = AudioManager.baseInitWavesurfer(before_step);
    
    AudioManager.eventHandlers[step+'-make-compand'] = function () {
        AudioManager.shadowBody();
        $.post('/audio/first_compression_make_compand', {
                    hash: AudioManager.hash,
                    edition: AudioManager.FirstCompressionEdition,
                    filename: AudioManager.currentFilename
        }, function (data) {
                if (data.success) {
                    AudioManager.unshadowBody();
                    $('#'+after_step+'-step').show();
                    AudioManager.setCurrentFilename(data.filename);
                    AudioManager.AfterFirstCompressionPlayer = AudioManager.baseInitWavesurfer(after_step);
                    AudioManager.FirstCompressionEdition++;      
                }        
        }, 'json');    
            
    };
    AudioManager.eventHandlers[step+'-next-step'] = function () {
        AudioManager.BeforeFirstCompressionPlayer.pause();
        AudioManager.AfterFirstCompressionPlayer.pause();
        AudioManager.nextStep();        
    };   
}

AudioManager.initChooseMusicStep = function () {
    var step = AudioManager.currentStep;
    var before_step = 'before-' + step;
    var after_step  = 'after-' + step;
    
    if (prev_step = AudioManager.getPrevStep()) {
        $('#'+prev_step+'-step').hide();    
    }
    $('#'+step+'-step').show();
    
    AudioManager.hideUndoButton();
    
    $('#'+before_step+'-fileupload').fileupload({
        url: '/audio/choose_music_upload',
        formData: {hash: AudioManager.hash},
        dataType: 'json',
        done: function (e, data) {
            $('#'+after_step+'-step').show();
            $('#'+before_step+'-progress').hide();
            AudioManager.setCurrentBackgroundFilename(data.result.filename);
            AudioManager.ChooseMusicPlayer = AudioManager.baseInitWavesurfer(after_step, true);   
        },
        progressall: function (e, data) {
            var progress = parseInt(data.loaded / data.total * 100, 10);
            $('#'+before_step+'-progress').show();
            $('#'+before_step+'-progress .progress-bar').css('width', progress + '%');
        }
    }).prop('disabled', !$.support.fileInput)
        .parent().addClass($.support.fileInput ? undefined : 'disabled');
        
    AudioManager.eventHandlers[step+'-next-step'] = function () {
        AudioManager.ChooseMusicPlayer.pause();
        AudioManager.nextStep();        
    };   
}

AudioManager.initMergeTracksStep = function () {
    var step = AudioManager.currentStep;
    var before_step = 'before-' + step;
    var after_step  = 'after-' + step;
    
    if (prev_step = AudioManager.getPrevStep()) {
        $('#'+prev_step+'-step').hide();    
    }
    $('#'+step+'-step').show();
    
    AudioManager.hideUndoButton();
    
    AudioManager.BeforeMergeTracksTrackPlayer      = AudioManager.baseInitWavesurfer(before_step+'-track');
    AudioManager.BeforeMergeTracksBackgroundPlayer = AudioManager.baseInitWavesurfer(before_step+'-background', true, true);
    AudioManager.AfterMergeTracksPlayer            = null;
    
    AudioManager.TrackVolume = 0.50;
    AudioManager.BackgroundVolume = 0.50;
    
    $('#'+before_step+'-track-volume').slider({
        orientation: "vertical",
        range: "min",
        min: 0,
        max: 100,
        value: AudioManager.TrackVolume*100,
        slide: function(event, ui) {
            AudioManager.TrackVolume = ui.value/100;
            AudioManager.BeforeMergeTracksTrackPlayer.backend.setVolume(AudioManager.TrackVolume);
        }
    });
    $('#'+before_step+'-background-volume').slider({
        orientation: "vertical",
        range: "min",
        min: 0,
        max: 100,
        value: AudioManager.BackgroundVolume*100,
        slide: function(event, ui) {
            AudioManager.BackgroundVolume = ui.value/100;
            AudioManager.BeforeMergeTracksBackgroundPlayer.backend.setVolume(AudioManager.BackgroundVolume);
        }
    });
    
    AudioManager.initEventListeners(before_step);
    
    AudioManager.eventHandlers[before_step+'-play'] = function () { 
        AudioManager.BeforeMergeTracksTrackPlayer.playPause();
        AudioManager.BeforeMergeTracksBackgroundPlayer.playPause();      
    };
    AudioManager.eventHandlers[step+'-cut-background'] = function () {
        if (AudioManager.isBeginAndEndCorrect(AudioManager.BeforeMergeTracksBackgroundPlayer.markers.begin, AudioManager.BeforeMergeTracksBackgroundPlayer.markers.end)) {
            var begin = AudioManager.BeforeMergeTracksBackgroundPlayer.markers.begin.position;
            var end   = AudioManager.BeforeMergeTracksBackgroundPlayer.markers.end.position;
            
            AudioManager.shadowBody();
            
            $.post('/audio/merge_tracks_cut_background', {
                        hash: AudioManager.hash,
                        begin: begin,
                        end: end,
                        edition: AudioManager.MergeTracksEdition,
                        filename: AudioManager.currentBackgroundFilename
            }, function (data) {
                    if (data.success) {
                        AudioManager.unshadowBody();
                        
                        AudioManager.setPreviousFilename(AudioManager.currentBackgroundFilename);
                        AudioManager.setCurrentBackgroundFilename(data.filename);
                        AudioManager.updateUndoButton(true);
                        
                        AudioManager.BeforeMergeTracksBackgroundPlayer.load(data.filename);
                        AudioManager.MergeTracksEdition++;
                        AudioManager.BeforeMergeTracksBackgroundPlayer.backend.setVolume(AudioManager.BackgroundVolume);     
                    }        
            }, 'json');
        }    
            
    };
    AudioManager.eventHandlers[step+'-make-fading'] = function () {
        if (AudioManager.isBeginAndEndCorrect(AudioManager.BeforeMergeTracksBackgroundPlayer.markers.begin, AudioManager.BeforeMergeTracksBackgroundPlayer.markers.end)) {
            var begin = AudioManager.BeforeMergeTracksBackgroundPlayer.markers.begin.position;
            var end   = AudioManager.BeforeMergeTracksBackgroundPlayer.markers.end.position;
            
            AudioManager.shadowBody();
            
            $.post('/audio/merge_tracks_make_fading', {
                        hash: AudioManager.hash,
                        begin: begin,
                        end: end,
                        edition: AudioManager.MergeTracksEdition,
                        filename: AudioManager.currentBackgroundFilename
            }, function (data) {
                    if (data.success) {
                        AudioManager.unshadowBody();
                        
                        AudioManager.setPreviousFilename(AudioManager.currentBackgroundFilename);
                        AudioManager.setCurrentBackgroundFilename(data.filename);
                        AudioManager.updateUndoButton(true);
                        
                        AudioManager.BeforeMergeTracksBackgroundPlayer.load(data.filename);
                        AudioManager.MergeTracksEdition++; 
                        AudioManager.BeforeMergeTracksBackgroundPlayer.backend.setVolume(AudioManager.BackgroundVolume);     
                    }        
            }, 'json');
        }    
            
    };
    AudioManager.eventHandlers[step+'-make-merge'] = function () {
        AudioManager.shadowBody();
        $.post('/audio/merge_tracks_make_merge', {
                    hash: AudioManager.hash,
                    edition: AudioManager.MergeTracksEdition,
                    filename: AudioManager.currentFilename,
                    background_filename: AudioManager.currentBackgroundFilename,
                    track_volume: AudioManager.TrackVolume,
                    background_volume: AudioManager.BackgroundVolume
        }, function (data) {
                if (data.success) {
                    AudioManager.unshadowBody();
                    $('#'+after_step+'-step').show();
                    AudioManager.hideUndoButton();
                    AudioManager.setCurrentFilename(data.filename);
                    AudioManager.AfterMergeTracksPlayer = AudioManager.baseInitWavesurfer(after_step);
                    AudioManager.MergeTracksEdition++;      
                }        
        }, 'json');    
    }
    AudioManager.eventHandlers[step+'-next-step'] = function () {
        AudioManager.BeforeMergeTracksTrackPlayer.pause();
        AudioManager.BeforeMergeTracksBackgroundPlayer.pause();
        AudioManager.AfterMergeTracksPlayer.pause();
        AudioManager.nextStep();        
    };   
}

AudioManager.initSecondCompressionStep = function () {
    var step = AudioManager.currentStep;
    var before_step = 'before-' + step;
    var after_step  = 'after-' + step;
    
    if (prev_step = AudioManager.getPrevStep()) {
        $('#'+prev_step+'-step').hide();    
    }
    $('#'+step+'-step').show();
    
    AudioManager.hideUndoButton();
    
    AudioManager.BeforeSecondCompressionPlayer = AudioManager.baseInitWavesurfer(before_step);
    
    AudioManager.eventHandlers[step+'-make-compand'] = function () {
        AudioManager.shadowBody();
        $.post('/audio/second_compression_make_compand', {
                    hash: AudioManager.hash,
                    edition: AudioManager.SecondCompressionEdition,
                    filename: AudioManager.currentFilename
        }, function (data) {
                if (data.success) {
                    AudioManager.unshadowBody();
                    $('#'+after_step+'-step').show();
                    AudioManager.setCurrentFilename(data.filename);
                    AudioManager.AfterSecondCompressionPlayer = AudioManager.baseInitWavesurfer(after_step);
                    AudioManager.SecondCompressionEdition++;      
                }        
        }, 'json');    
            
    };
    AudioManager.eventHandlers[step+'-next-step'] = function () {
        AudioManager.BeforeSecondCompressionPlayer.pause();
        AudioManager.AfterSecondCompressionPlayer.pause();
        AudioManager.nextStep();        
    };   
}

AudioManager.initDownloadStep = function () {
    var step = AudioManager.currentStep;
    
    if (prev_step = AudioManager.getPrevStep()) {
        $('#'+prev_step+'-step').hide();    
    }
    $('#'+step+'-step').show();
    
    AudioManager.hideUndoButton();
    
    AudioManager.initEventListeners(step);
    
    AudioManager.eventHandlers[step+'-file'] = function () {
        AudioManager.shadowBody();
        $.post('/audio/download_prepare_file', {
                    hash: AudioManager.hash,
                    filename: AudioManager.currentFilename
        }, function (data) {
                if (data.success) {
                    AudioManager.unshadowBody();
                    $.fileDownload('/audio/download_file?filename='+data.filename);          
                }        
        }, 'json');    
            
    };
}

AudioManager.isBeginAndEndCorrect = function (begin, end) {
    if (begin) {
        if (end) {
            return true;
            
        } else {
            alert('Маркер конца не установлен');
            
        }    
    } else {
        alert('Маркер начала не установлен');
    }
    return false;    
}

AudioManager.nextStep = function () {   
    var step = AudioManager.getNextStep();
    AudioManager.setCurrentStep(step);
    AudioManager.initPlayer();   
};

AudioManager.getNextStep = function () {
    for (var i = 0; i < AudioManager.steps.length; i++) {
        if (AudioManager.steps[i] == AudioManager.currentStep) {
            break;    
        }    
    }
    if (step = AudioManager.steps[++i]) {
        return step;
    }
    return null;        
}

AudioManager.getPrevStep = function () {
    for (var i = 0; i < AudioManager.steps.length; i++) {
        if (AudioManager.steps[i] == AudioManager.currentStep) {
            break;    
        }    
    }
    if (step = AudioManager.steps[--i]) {
        return step;
    }        
    return null;
}

AudioManager.shadowBody = function () {
    $('.body-overlay').css('height', AudioManager.getDocHeight()).show();;
}

AudioManager.unshadowBody = function () {
    $('.body-overlay').hide();
}

AudioManager.getDocHeight = function () {
    var D = document;
    return Math.max(
        Math.max(D.body.scrollHeight, D.documentElement.scrollHeight)//,
        //Math.max(D.body.offsetHeight, D.documentElement.offsetHeight),
        //Math.max(D.body.clientHeight, D.documentElement.clientHeight)
    );
}

AudioManager.undo = function () {
    // TODO сделать покрасивше

    var filename = AudioManager.previousFilename;
    if (AudioManager.currentStep == 'remove-noise') {
        AudioManager.setCurrentFilename(filename);
        AudioManager.RemoveNoisePlayer.load(filename);
        AudioManager.RemoveNoiseEdition--;
    } else if (AudioManager.currentStep == 'first-compression') {
        // noting
    } else if (AudioManager.currentStep == 'choose-music') {
        // noting
    } else if (AudioManager.currentStep == 'merge-tracks') {
        AudioManager.setCurrentFilename(filename);
        AudioManager.BeforeMergeTracksBackgroundPlayer.load(filename);
        AudioManager.MergeTracksEdition--;
    } else if (AudioManager.currentStep == 'second-compression') {
        // noting 
    } else if (AudioManager.currentStep == 'download') {
        // noting 
    }  
    
    AudioManager.hideUndoButton();  
}

AudioManager.updateUndoButton = function (is_background) {
    filename = AudioManager.currentFilename;
    if (is_background) {
        filename = AudioManager.currentBackgroundFilename;
    }
    
    if (filename && (AudioManager.previousFilename != filename)) {
        AudioManager.showUndoButton();    
    } else {
        AudioManager.hideUndoButton();   
    }        
}

AudioManager.hideUndoButton = function () {
    $('#back-step').hide();    
}

AudioManager.showUndoButton = function () {
    $('#back-step').show();    
}


